package org.knot.mockejb3.annotation;

import java.lang.annotation.Annotation;
import java.util.Map;

import org.knot.mockejb3.annotation.lifeCycle.Remove;
import org.knot.mockejb3.interceptor.Interceptor;
import org.knot.mockejb3.interceptor.InvocationContext;

@SuppressWarnings("unchecked")
public class StateFulBeanManager implements Interceptor {


    public void intercept(InvocationContext invocationContext) throws Exception {

        invocationContext.proceed();
        
        //如果是调用带Remove注释的将该对象从容器中销毁；
        Class remove = this.getClass().getClassLoader().loadClass("javax.ejb.Remove");
        System.out.println("=========="+invocationContext.getTargetMethod().getName());
        Annotation annotation = invocationContext.getTargetMethod().getAnnotation(remove);

        if (null != annotation) {
            Map<AnnotationRule, RuleParams> mm = AnnotationRuleManager.getInstance().getClasseAnnotations().get(
                    invocationContext.getProxyObject()).getAnnotations();

            for (AnnotationRule rule : mm.keySet()) {
                // remove
                if (rule instanceof  Remove) {
                    RuleParams params = mm.get(rule);
                    rule.invoke(params);
                }
            }
        }
    }

}
